//
//  videoTableViewController.m
//  A1.0
//
//  Created by lanou3g on 15-3-26.
//  Copyright (c) 2015年 ccandxx. All rights reserved.
//

#import "videoTableViewController.h"
#import "SDRefresh.h"
#import "videoModel.h"
#import "videoTableViewCell.h"
#import "CommonAPI.h"

#import "UIImageView+WebCache.h"
#import "UIImageView+HighlightedWebCache.h"
#import "SDWebImageManager.h"
#import "SDWebImageDecoder.h"

#import "playViewController.h"


#import "popupView.h"
#import "UIViewController+LewPopupViewController.h"
#import "LewPopupViewAnimationFade.h"
#import "LewPopupViewAnimationSlide.h"
#import "LewPopupViewAnimationSpring.h"
#import "LewPopupViewAnimationDrop.h"

#import "Reachability.h"
@interface videoTableViewController ()
@property (nonatomic, weak) SDRefreshFooterView *refreshFooter;
@property (nonatomic, assign) NSInteger totalRowCount;
@property (nonatomic, strong) NSMutableArray *dataArray;



//用于记录当前的cell的indexPath
@property (nonatomic,strong )NSIndexPath *index;

//用于检测网络
@property (nonatomic ,strong)Reachability *r;

@end

@implementation videoTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        self.title = @"上拉和下拉刷新";
        self.tableView.rowHeight = 60.0f;
        self.tableView.separatorColor = [UIColor whiteColor];
    
        
    }
    return self;
}

- (void)viewDidLoad {
    
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.r = [Reachability reachabilityWithHostname:@"www.baidu.com"];
    
    if  ([self.r currentReachabilityStatus]== NotReachable){
        
        
        
        
        
        
        
        
    }else{

    [self setupHeader];
    [self setupFooter];
    __weak typeof (self) weakSelf = self;
    [CommonAPI firstVideoListData:^(id data) {
        weakSelf.dataArray = data;
        [weakSelf.tableView reloadData];
    }];
    
}
}


- (void)setupHeader
{
    SDRefreshHeaderView *refreshHeader = [SDRefreshHeaderView refreshView];
    
    // 默认是在navigationController环境下，如果不是在此环境下，请设置 refreshHeader.isEffectedByNavigationController = NO;
    [refreshHeader addToScrollView:self.tableView];
    
    __weak SDRefreshHeaderView *weakRefreshHeader = refreshHeader;
    refreshHeader.beginRefreshingOperation = ^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
       [weakRefreshHeader endRefreshing];
        });
    };
    
    // 进入页面自动加载一次数据
    //[refreshHeader beginRefreshing];
}


- (void)setupFooter
{
    SDRefreshFooterView *refreshFooter = [SDRefreshFooterView refreshView];
    [refreshFooter addToScrollView:self.tableView];
    [refreshFooter addTarget:self refreshAction:@selector(footerRefresh)];
    _refreshFooter = refreshFooter;
}
- (void)footerRefresh
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      
        [self.refreshFooter endRefreshing];
    });
    videoModel *m = self.dataArray.lastObject;
    __weak typeof (self) weakSelf = self;
    [CommonAPI downVideoListstring:m.cTime Data:^(id data) {
        NSArray *array = data;
        [weakSelf.dataArray addObjectsFromArray:array];
        [weakSelf.tableView reloadData];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    videoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"videocell" forIndexPath:indexPath];
    videoModel *m = self.dataArray[indexPath.row];
    cell.videoLabel.text = m.title;
    
    
    NSURL *picurl = [NSURL URLWithString:m.pic];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    [cell.videoImage sd_setImageWithURL:picurl placeholderImage:[UIImage imageNamed:@"cat.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        cell.videoImage.image = image;
    }];
    
   
    self.index = indexPath;
    
    [cell.playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
     
    return cell;
}

- (void)playButtonAction:(UIButton *)sender
{
    
    playViewController *playVC = [[playViewController alloc]init];
    
    playVC.model = self.dataArray[_index.row];
    
    [self.navigationController pushViewController:playVC animated:YES];
    
    
    
}


//返回cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    videoModel *m = [[videoModel alloc]init];
    m = self.dataArray[indexPath.row];
    
    return [videoTableViewCell cellFloatWithModel:m];

    
}
- (IBAction)sss:(id)sender {
    popupView *view = [popupView defaultPopupView];
    view.parentVC = self;
    
    [self lew_presentPopupView:view animation:[LewPopupViewAnimationDrop new] dismissed:^{
        NSLog(@"动画结束");
    }];
}
@end
